package com.czl.module_service.adapter.asset

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.PurchaseOrderBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemPurchaseOrderListBinding
import com.czl.module_service.fragment.assetUse.PurchaseOrderChooseFragment

class PurchaseOrderAdapter(private val fragment: PurchaseOrderChooseFragment) :
    BaseQuickAdapter<PurchaseOrderBean, BaseDataBindingHolder<ItemPurchaseOrderListBinding>>(
        R.layout.item_purchase_order_list
    ) {

    var facilityIdList: ArrayList<Int> = arrayListOf()
    var lastPosition: Int = -1

    override fun convert(
        holder: BaseDataBindingHolder<ItemPurchaseOrderListBinding>,
        item: PurchaseOrderBean,
    ) {
        item.position = holder.layoutPosition
        holder.dataBinding?.run {
            data = item
            adapter = this@PurchaseOrderAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is PurchaseOrderBean) {
            fragment.startContainerActivity(AppConstants.Router.Service.F_SERVICE_PURCHASE_SORT_LIST, Bundle().apply {
                putSerializable(AppConstants.BundleKey.PURCHASE_CARD_KEY, it.purchaseCardId)
            })
        }
    })

    val onChoosedSort : BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is PurchaseOrderBean) {
            if (lastPosition == -1) {
                val item: PurchaseOrderBean = it
                item.selected = !item.selected
                setData(item.position!!, item)
            } else if (it.position!! == lastPosition) {
                val item: PurchaseOrderBean = it
                item.selected = !item.selected
                setData(item.position!!, item)
            } else {
                val item: PurchaseOrderBean = data[lastPosition]
                item.selected = !item.selected
                setData(lastPosition, item)
                val item2: PurchaseOrderBean = it
                item2.selected = !item2.selected
                setData(item2.position!!, item2)
            }
            lastPosition = it.position!!

        }
    })
}